package com.zhouxiaoge.socket.serversocket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author 周明阳
 */
public class SocketClient {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

        //客户端
        //1、创建客户端Socket，指定服务器地址和端口
        Socket socket = new Socket("127.0.0.1", 8888);
        //2、获取输出流，向服务器端发送信息
        System.out.println("请输入要转化的字母: ");
        String s = scanner.nextLine();
        //字节输出流
        OutputStream os = socket.getOutputStream();
        os.write(s.getBytes());
        //将输出流包装成打印流
        System.out.println("等待服务器返回．．．．");
        socket.shutdownOutput();
        //3、获取输入流，并读取服务器端的响应信息
        InputStream is = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int read = is.read(bytes);
        System.out.println("我是客户端，服务器说：" + new String(bytes, 0, read));
        //4、关闭资源
        is.close();
        os.close();
        socket.close();
    }
}
